package com.system.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.system.model.Teacher;
import com.system.service.ScoreService;
import com.system.service.TeacherService;
import com.system.util.ReflectUtil;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class studentServlet
 */
public class TeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TeacherServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("gbk");
		response.setCharacterEncoding("utf-8");
		response.setHeader("content-type", "text/html; charset=utf-8");
		response.setHeader("Access-Control-Allow-Origin", "*"); 
		ReflectUtil.invokeMethods(this.getClass(), request, response);
	}
	/**
	 * 查询所有老师信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void queryAll(HttpServletRequest request, HttpServletResponse response) throws Exception{
		TeacherService service=new TeacherService();
		JSONObject json=service.queryAll();
		response.getWriter().write(json.toString());
	}
	/**
	 * 分页查询教师信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void queryAllbyNum(HttpServletRequest request, HttpServletResponse response)throws Exception{
		Integer pageNum=Integer.parseInt(request.getParameter("pageNum"));
		Integer amount=Integer.parseInt(request.getParameter("amount"));
		TeacherService service=new TeacherService();
		JSONObject json=service.checkoutQueryall(pageNum, amount);
		response.getWriter().write(json.toString());
	}
	/**
	 * 教师根据自己的职工号查询所教课程的所有学生成绩
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void queryScore(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String teanum=request.getParameter("teanum");
		ScoreService service=new ScoreService();
		JSONObject json=service.checkoutQueryTeaScores(teanum);
		response.getWriter().write(json.toString());
	}
	/**
	 * 老师查询信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void queryTeacherByteanum(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String str=request.getParameter("teanum");
		TeacherService service=new TeacherService();
		JSONObject json=service.queryInfo(str);
		response.getWriter().write(json.toString());
	}
	/**
	 * 添加老师信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void addTeacher(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String teachername = request.getParameter("teachername");
		String major = request.getParameter("major");
		String teanum = request.getParameter("teanum");
		String coursename=request.getParameter("coursename");
		
		//【注】user_id是关联user表的外键，依赖于user表的自动生成，添加操作不用在前端参数中拿
//		String str=request.getParameter("user_id");
		//todo solve exception
//		int user_id = Integer.parseInt(str);
		Teacher tea = new Teacher();
		tea.setMajor(major);
		tea.setTeanum(teanum);
		tea.setTeachername(teachername);
//		stu.setUser_id(user_id);
		tea.setUser_id(0);
		
		TeacherService service=new TeacherService();
		JSONObject json=service.add(tea,coursename);
		response.getWriter().write(json.toString());
	}
	/**
	 * 修改老师信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void deleteTeacher(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String[] ids = request.getParameterValues("userIds");
		//todo solve exception
		int[] userIds = new int[ids.length];
		for(int i = 0; i < ids.length; i++) {
			userIds[i] = Integer.parseInt(ids[i]);
		}
		
		TeacherService service=new TeacherService();
		JSONObject json=service.deleteTeachers(userIds);
		response.getWriter().write(json.toString());
	}
	/**
	 * 删除老师信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void modifyTeacher(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String teachername = request.getParameter("teachername");
		String major = request.getParameter("major");
		String teanum = request.getParameter("teanum");
		String str=request.getParameter("user_id");
		//todo solve exception
		int user_id = Integer.parseInt(str);
		
		Teacher tea = new Teacher();
		tea.setMajor(major);
		tea.setTeanum(teanum);
		tea.setTeachername(teachername);
		tea.setUser_id(user_id);
		
		TeacherService service=new TeacherService();
		JSONObject json=service.modify(tea);
		response.getWriter().write(json.toString());
	}
	/**
	 * 教师录入自己所教课程的学生成绩
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void insertScore(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String teanum=request.getParameter("teanum");
		String stunum=request.getParameter("stunum");
		String coursename=request.getParameter("coursename");
		Integer stu_score=Integer.parseInt(request.getParameter("score"));
		ScoreService service=new ScoreService();
		JSONObject json=new JSONObject();
		json=service.checkoutInsertScore(teanum, stunum, coursename, stu_score);
		response.getWriter().write(json.toString());
	}
	
	public void lookoutRemarks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String teanum=request.getParameter("teanum");
		TeacherService service=new TeacherService();
		JSONObject json=new JSONObject();
		json=service.checkoutQueryRemarks(teanum);
		response.getWriter().write(json.toString());
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}

